<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="4.8" v-loading="loading" v-for="(item,index) in planList">
        <el-card class="box-card" :key="'node_'+item.id">
          <div slot="header" class="clearfix">
            <el-button class="el-icon-delete" type="text" @click="handleDelete(item)">删除</el-button>
            <el-button class="el-icon-edit" type="text" @click="handleUpdate(item)">修改</el-button>
            <el-button class="el-icon-plus" type="text" @click="handleNodeTask(item,'2')">配置作业</el-button>
          </div>
          <div class="box-content">
            {{item.nodeName }}
          </div>
        </el-card>
      </el-col>
    </el-row>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改(教育)绘本计划管理(one，two，three) 每天内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="DAYS名词" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="DAYS" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listNode,
    getNode,
    delNode,
    addNode,
    updateNode,
    setNodeSortNo
  } from "@/api/bus_web/course/node";

  export default {
    name: "Plan",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        pictureBookId: this.$route.query.id,
        // 总条数
        total: 0,
        // (教育)绘本计划管理(one，two，three) 每天内容表格数据
        planList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          pictureBookId: this.pictureBookId,
          nodeType: 3
        },
        // 表单参数
        form: {
          pictureBookId: this.pictureBookId,
          nodeType: 3
        },
        // 表单校验
        rules: {
          nodeName: [{
            required: true,
            message: "DAYS名称不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {

      this.getList();
    },
    methods: {
      /** 查询(教育)绘本计划管理(one，two，three) 每天内容列表 */
      getList() {
        this.loading = true;
        this.queryParams.pictureBookId = this.pictureBookId
        listNode(this.queryParams).then(response => {
          this.planList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          pictureBookId: null,
          planName: null,
          nodeType: 3,
          sortNo: null,
          gmtCreate: null,
          gmtModify: null,
          deleted: null,
          createBy: null,
          updateBy: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加DAYS";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getNode(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改DAYS";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.pictureBookId = this.pictureBookId
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateNode(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addNode(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
          return delNode(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('course/plan/export', {
          ...this.queryParams
        }, `plan_${new Date().getTime()}.xlsx`)
      },
      /*****nodeType:1-课件,2-作业*****/
      handleNodeTask(row, nodeType) {
        let title = '课件配置'
        if (2 == nodeType) {
          title = '作业配置'
        }
        // let url = '/course/picturebook/node/' + row.pictureBookId + "/" + nodeType+"?pNodeId="+row.id
        // this.$tab.openPage(title, url);
        let url = '/course/picturebooknode'
        let params = {
          "id": row.pictureBookId,
          "pNodeId": row.id,
          "type": nodeType
        }

        this.$tab.openPage(title, url, params);
      },


    }
  };
</script>
